package com.hgts.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hgts.pojo.Details;
import com.hgts.service.IDetailsService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

public class MyJob implements Job {

    @Autowired
    private IDetailsService detailsService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // 在这里编写需要定时执行的业务逻辑
        LambdaQueryWrapper<Details> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Details::getIsDelete,0);
        List<Details> detailsList = detailsService.list(queryWrapper);
        for (Details details : detailsList) {
            String endTime = details.getEndTime();
            long time = new Date(endTime).getTime();
            //获取当前系统时间
            long timeMillis = System.currentTimeMillis();
            //判断是否到达还书时间
            if (timeMillis > time){
                //过期加入黑名单
                LambdaUpdateWrapper<Details> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.set(Details::getIsPromise,1);
                detailsService.update(queryWrapper);
            }
        }

    }
}